package xml;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;


public class XMLFileReader{
	//creates the array list to contain files
	private ArrayList<File> filesList = new ArrayList<File>();
//	private String[] files;//array list of files
	private JFileChooser chooser = new JFileChooser();
	/**
	 * creates the FileReader and an array list of files
	 */
	public XMLFileReader() {
		
		//gets one of the files
		JOptionPane.showMessageDialog(null, "Select one of the tabulation files.");
		chooser.showOpenDialog(null);
		String votLoc = chooser.getSelectedFile().toString();
		String ext = votLoc.substring((votLoc.lastIndexOf('.')+1), votLoc.length());
		while (!(ext.equals("vot"))) {
			JOptionPane.showMessageDialog(null, "You didn't select a tabulation file. Tabulation files are .vot files.");
			JOptionPane.showMessageDialog(null, "Select one of the tabulation files.");
			chooser.showOpenDialog(null);
			votLoc = chooser.getSelectedFile().toString();
			ext = votLoc.substring((votLoc.lastIndexOf('.')+1), votLoc.length());
		}
		
		Object[] options = {"Automatic","Manual"};
		int choice = JOptionPane.showOptionDialog(null,"Select \"Automatic\" to automatically tabulate all of the .vot files in the same folder as the one you have selected.\nWarning - if you choose \"Automatic\" there cannot be any .vot files in the folder not related to tabulation\nUnless the warning applies \"Automatic\" is recommended","Manual or Automatic selection?",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[1]);
		if(choice == 0){
			autoRead();
		}else{
			File toBeAdded1 = chooser.getSelectedFile();
			String path = toBeAdded1.getPath();
			filesList.add(new File(path));
			manualRead();
		}
	}
	
	/**
	 * @return the array list containing all the files
	 */
	public ArrayList<File> getFiles(){
		return filesList;
	}
	/**
	 * if the user for some odd reason chooses manual tabulate they have to click through all the files on their own this allows them to do that
	 */
	public void manualRead(){
		int done = 1;
		while(done == 1){
			JOptionPane.showMessageDialog(null, "Choose the next tabulation file.");
			chooser.showOpenDialog(null);
			
			Object[] options = {"Delete last choice and move on.","Move on without deleting."};
			int mistake = JOptionPane.showOptionDialog(null,"If you just accidentally clicked the wrong file, select \"Delete last choice and move on\" to delete it, otherwise select \"Move on without deleting\".","Mistake Corrector",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[1]);
		
			if(mistake == 1){
				//for some reason i can't just add the file it works better if i add a new file using the file path
				File toBeAdded = chooser.getSelectedFile();
				String pathx = toBeAdded.getPath();
				
				filesList.add(new File(pathx));
			}
			
			Object[] options2 = {"Done","Not Done"};
			done = JOptionPane.showOptionDialog(null,"Are you done?","Done?",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options2,options2[1]);
		}
	}
	/**
	 * If the user selects automatic reader this method is called which takes in all of the vot files in that folder 
	 */
	public void autoRead(){
		//gets the directory of the folder containing all the files
		File parent = chooser.getSelectedFile().getParentFile();
		System.out.println("parent is: " + parent);

		//creates a list of all the file paths
		String[] files = parent.list();
		
		//gives the list of files to the array list
		for(int i = 0; i<files.length;i++){
			System.out.println(files[i]);
			if(files[i].endsWith(".vot")){
				filesList.add(new File(parent.toString() + File.separator + files[i]));
			}
		}
		
		//watch it work
		/*for(int i = 0;i<filesList.size();i++){
			System.out.println(filesList.get(i));
		}*/
	}
}
